package com.polaris.live.popup

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.widget.LinearLayout
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.manager.CoroutineScopeManager
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.PopupSetTranslationBinding
import com.polaris.live.manager.AssetsConfigManager
import com.polaris.live.net.userService
import com.polaris.live.resp.go_resp.InfoModifyBean
import com.polaris.live.translation.R.string
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * SetTranslationPopup
 *
 * @author Created by 半仙 on 2023/11/17/017 21:29
 */
class SetTranslationPopup(
    context: Context,
    private val mIsShow: Boolean,
    private val mIsOpen: Boolean?,
    private val mViewModel: BaseViewModel,
    private val onSelectLanguage: ((language: String) -> Unit),
    private val onTranslationSwitch: ((switch: Boolean) -> Unit),
) : BottomPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.popup_set_translation
    }

    private lateinit var mBinding: PopupSetTranslationBinding

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupSetTranslationBinding.bind(popupImplView)
        if (mIsShow) {
            mBinding.llBottom.visibility = View.GONE
        }
        var language = "English"
        mViewModel.viewModelScope.launch(CoroutineScopeManager.ioDispatcher) {
            val data = AssetsConfigManager.readTranslation()
            withContext(CoroutineScopeManager.mainDispatcher) {
                data.forEach {
                    if (it.code == UserManager.getUser().profile?.spokenLanguage) {
                        language = "${it.language}-${it.country}"
                    }
                }
                mBinding.ivLanguage.text = language
            }
        }


        if (!TextUtils.isEmpty(language)) {
            mBinding.ivLanguage.text = language
        }

        if (mIsOpen == true) {
            mBinding.switchBtn.isChecked = true
        }

        mBinding.ivSelect.setOnSingleClickListener {
            XPopup.Builder(context)
                .moveUpToKeyboard(false)
                .isDestroyOnDismiss(true)
                .asCustom(SetLanguagePopup(context, { languageResp ->
                    languageResp?.language?.let { it1 ->
                        mViewModel.requestLoading({ userService.infoModify(InfoModifyBean(spokenLanguage = it1)) },
                            {
                                UserManager.getUser().profile?.spokenLanguage = it1
                                onSelectLanguage.invoke(it1)
                                mBinding.ivLanguage.text = languageResp.localName
                                ToastGravity.showGravity(StringUtils.getString(string.you_spokenLanguage_save_success))
                            },
                            { ToastGravity.showGravity(it.errorMsg) })

                    }
                }, mViewModel, true))
                .show()
        }
        mBinding.switchBtn.setOnCheckedChangeListener { buttonView, isChecked ->
            onTranslationSwitch.invoke(isChecked)
        }

        val navigationBarHeight = NavigationBarUtils.getNavigationBarHeight(context)
        val layout = mBinding.viView.layoutParams as LinearLayout.LayoutParams
        layout.bottomMargin = navigationBarHeight
        mBinding.viView.layoutParams = layout
    }
}